



#include"app_Oscilloscope_ui.h"

#include"gl_.h"

#include"mcu_.h"

#include<stdio.h>
#include<math.h>



// 当前示波器没有FFT的功能。最高采样率：500k

int timer_counter[function_num] = {
	2,
	5,
	10,
	20,
	50,
	100,
	200,
	500,
	1000,
	2000
};

char title[] = "Oscilloscope";      //标题
char xtitle[function_num][16] = {
	"     ",
	"     ",
	"     ",
	"     ",
	"     ",
	"     ",
	"     ",
	"     ",
	"     ",
	"     ",
};
char xtitlexxx[function_num][16] = {
	// ADC 定时器中断时间。 经计算：f1型号，ADC转换，1.5周期最快约800k，7.5周期最快约600k采样率。
	// 这里就2us定时器触发ADC转换，加DMA缓存ADC的方案。  
	// 下面的时间timer 2us 表示一个点的时间是2us，这里对应的采样速率是500kHz（最高采样率）。
	// 定时器分频到1M， 下面是对应的定时器计数。  如果采样率是200，需要两秒才能刷新全屏，这样可以统一dma中断刷屏。
	"timer 2us  ",  // 500k >> 2
	"timer 5us  ",  // 200k
	"timer 10us ",  // 100k
	"timer 20us ",  // 50k
	"timer 50us ",  // 20k
	"timer 100us", // 10k
	"timer 200us", // 5k
	"timer 500us", // 2k
	"timer 1ms  ",  // 1k
	"timer 2ms  ",  // 500
};         //x轴标题
char y1title[] = "ch0 (V)";          //y轴标题
char y2title[] = "ch1 (V)";          //y轴标题

Button button0;
Button button1;
Button button2;

/// <summary>
/// 设置io口的输出值。在mcu.c中实现
/// 具体的实现方法和效果由用户自定义。如果是pwm口，则是设置占空比0-100。
/// </summary>
/// <param name="io">引脚号</param>
/// <param name="value">引脚值</param>
//void mcu_set_io(gpio* io, unt32 value) {}

#include"gl_.h"
TwoFunctionPicture tp;
	TextFont tf = {
	16,
	0,  // 默认透明
	RGB_white,  //背景
	RGB_black   //文本
	};

/*
0：adc采样周期选择 ad T
1：dac波形频率选择，按键都是长按有效。da F
2：dac波形选择，三种波形切换：正弦，三角，方波， da M   // sin wave tri wave squ wave   
*/
unsigned int button_mode = function_num * 2000;  
unsigned int timer_counter_index = function_num * 2000+4;  // 选择采样周期
unsigned int dac_frequency = 200;  // dac 的频率。 按键按下以10为步进梯度。 100ms检测一次按键，长按1秒步进100Hz，不需要下面的数组步进。
unsigned int wave_mode = function_num * 2000;

// unsigned int DAC_timer_counter = function_num * 2000;  
// dac转换时间：单位是us，时钟是2M，数组是100个浮点。频率从10 到 10k，对应的频率 f = 2,000,000/100/DAC_timer_counter =  20,000/DAC_timer_counter


unt16 dac_value[DAC_LENGTH];
// mode =0正弦，=1三角，=2：方波，数组的值在0 -- 4095.
void init_dac_array(int mode)
{
	static int yuan_mode = 3330;

	if (yuan_mode == mode)return;


	if (mode == 0) {
		for (int i = 0; i < DAC_LENGTH; i++) {
			dac_value[i] = (sinf(i * 3.14159f * 2 / DAC_LENGTH) + 1) * 2000;
		}
	}
	if (mode == 1) {
		for (int i = 0; i < DAC_LENGTH; i++) {
			dac_value[i] = i * 4000 / DAC_LENGTH;
		}
	}
	if (mode == 2) {
		for (int i = 0; i < DAC_LENGTH; i++) {
			dac_value[i] = i > DAC_LENGTH / 2 ? 4000 : 0;
		}
	}
	yuan_mode = mode;
}
//

//int dac_timer_counter[function_num] = {
//	2,   // f = 10 khz
//	5,   // f = 4 khz
//	10,   // f = 2 khz
//	20,   // f = 1 khz
//	50,   // f = 400 hz
//	100,   // f = 200 hz
//	200,   // f = 100 hz
//	500,   // f = 40 hz
//	1000,   // f = 20 hz
//	2000   // f = 10 hz   //
//};

void flush_msg()
{
	char str[] = "xxxs ad:xxxxus da:xxxxhz [sin,tri,squ][0]  mode=  ";
	int s = mcu_time_ms() / 1000;
	uint_to_str(s, str, 3, 10);
	uint_to_str(timer_counter[timer_counter_index % function_num], str + 8, 4, 10);
	if (dac_frequency < 10)dac_frequency = 10;
	if (dac_frequency > 10000)dac_frequency = 10000;
	uint_to_str(dac_frequency, str + 18, 4, 10);
	str[39] = '0' + wave_mode % 3;
	str[49] = '0' + button_mode % 3;
	draw_string_no_frame(0, 0, str, 50, tf);
}

//确认按键
void bbtn_check()
{
	button_mode++;
	flush_msg();
}

//up
void bbtn_up()
{
	if (button_mode % 3 == 0) {
		timer_counter_index++;
	}
	if (button_mode % 3 == 1) {
		dac_frequency += 10;
		dac_stop();

		dac_start();
	}
	if (button_mode % 3 == 2) {
		wave_mode++;
		dac_stop();
		init_dac_array(wave_mode % 3);
		dac_start();
	}

	flush_msg();
}

//dowm
void bbtn_down()
{
	if (button_mode % 3 == 0) {
		timer_counter_index--;
	}
	if (button_mode % 3 == 1) {
		dac_frequency -= 10;
		dac_stop();

		dac_start();
	}
	if (button_mode % 3 == 2) {
		wave_mode--;
		dac_stop();
		init_dac_array(wave_mode % 3);
		dac_start();
	}

	flush_msg();

}




void init_ui()
{
	// 初始化按键

	button_init(&button0, 10, 220, 30, 15);
	button_init(&button1, 50, 220, 30, 15);
	button_init(&button2, 90, 220, 30, 15);

	button_firstdraw(&button0);
	button_firstdraw(&button1);
	button_firstdraw(&button2);

	button0.onClick = bbtn_check;
	button1.onClick = bbtn_up;
	button2.onClick = bbtn_down;

}
//


unt16 adc_value_0[ADC_LENGTH]; // adc 原始值，16位整形
unt16 adc_value_1[ADC_LENGTH]; //  

float x_1[ADC_LENGTH] = { 0 };
float x_2[ADC_LENGTH] = { 0 };
float y_1[ADC_LENGTH] = { 0 };
float y_2[ADC_LENGTH] = { 0 };


// 刷新acd绘图。 
void flush_adc_curve()
{

	//for (int i = 0; i < ADC_LENGTH; i++) {
	//	TwoFunction_replace_point(&tp.draw, i, i, 5.3f * adc_value_0[i] / 4096, true);
	//}

	// 这里筛选 ADC_LENGTH/2 长度的片段显示，使得相位一致。
	int j;
	int static_x = 100;
	for (j = static_x; j < ADC_LENGTH / 2; j++) {
		if (adc_value_0[j - 1] < 1000 && adc_value_0[j + 1] > 1000) {
			break;
		}
	}
	for (int i = 0; i < ADC_LENGTH / 2; i++) {
		TwoFunction_replace_point(&tp.draw, i, i, 5.3f * adc_value_0[i + j - static_x] / 4096, true);
	}

	flush_msg();

}

// UI交互代码
void gui_test_240_400()
{
	gui_init();

	// 初始化数组
	for (int i = 0; i < ADC_LENGTH; i++) {
		x_1[i] = i;
		x_2[i] = i;
		y_1[i] = 0;
		y_2[i] = 0;
	}
	init_dac_array(1);

	int hadc_len = ADC_LENGTH / 2;

	TwoFunctionPicture_init(&tp, 0, 0, 400, 240);
	TwoFunctionPicture_draw_title(&tp, title, xtitle[0], y1title, y2title);
	TwoFunctionPicture_fast_draw_func(&tp, x_1, y_1, hadc_len, x_2, y_2, hadc_len, 0, 0,
		1,
		1, hadc_len,
		1, 3,
		1, 3
	);

	init_ui();
	int t = 0;
	int x, y;
	while (true)
	{
		t++;
		 mcu_delay_ms(1);
		if (t % 20 == 0) {
			//消息派遣
			if (GD_is_touched(&x, &y)) {
				dispatchMsg(x, y);
			}
			else {
				dispatchMsg(-1, -1);   // 这样才能使控件在没有按下的时候恢复显示。
				//	printf("b");
			}
		}

		float ft = t;
		float v = sinf((int)(ft / 1000 + 1) * 0.002f * t) + 1.1;
		adc_value_0[t % 1024] = v * 2000;

		if (t % 1000 == 1) flush_adc_curve();

		printf("%d ", t);
	}

}
//

void init_oscilloscope()
{
	gui_init();

	// 初始化数组
	for (int i = 0; i < ADC_LENGTH; i++) {
		x_1[i] = i;
		x_2[i] = i;
		y_1[i] = 0;
		y_2[i] = 0;
	}
//	init_dac_array(0);
	init_dac_array(wave_mode % 3);

	int hadc_len = ADC_LENGTH / 2;
	TwoFunctionPicture_init(&tp, 0, 0, 400, 240);
	TwoFunctionPicture_draw_title(&tp, title, xtitle[0], y1title, y2title);
	TwoFunctionPicture_fast_draw_func(&tp, x_1, y_1, hadc_len, x_2, y_2, hadc_len, 0, 0,
		1,
		1, hadc_len,
		1, 3,
		1, 3
	);
	init_ui();

}

























